package edu.chinasoft.servlet;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import javax.servlet.annotation.WebServlet;
import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSON;

import edu.chinasoft.pojo.Java01;

/**
 *
 * @author liuyunhe
 * @date 2020年2月22日:下午6:27:32
 * @version V1.0
 * @Description: 
 * 
*/
@WebServlet(name="myCreateJsonForServlet",value="/servlet/createjsonforservlet")
public class  CreateJsonForServlet  extends HttpServlet {
	private static Logger logger = Logger.getLogger( CreateJsonForServlet .class);
	
	/**
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		logger.debug(" CreateJsonForServlet >doGet start.......");
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		
		String[] categoriesArray= {"苹果","橘子","荔枝","桃子","栗子","梨子","柿子"};
		int[] dataArray= {500,280,386,190,107,207,452};
		
		Java01 java01 = new Java01(categoriesArray, dataArray);
		String result = JSON.toJSONString(java01, true);
		writeFile(this.getServletContext().getRealPath("/data/demo02.json"),result);
		
		response.sendRedirect(""+request.getContextPath()+"/echars02.jsp");
		
		
		logger.debug(" CreateJsonForServlet >doGet end.......");
	}
	
	public static void writeFile(String filePath, String sets)
	        throws IOException {
//		File file = new File(filePath);
//		file.createNewFile();
	    FileWriter fw = new FileWriter(filePath);
	    PrintWriter out = new PrintWriter(fw);
	    out.write(sets);
	    out.println();
	    fw.close();
	    out.close();
	}

	
	/**
	 *
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		logger.debug(" CreateJsonForServlet >doPost start.......");
		this.doGet(request, response);
		
		logger.debug(" CreateJsonForServlet >doPost end.......");
	}
	
}